<!-- 修改业务员 -->
<template>
  <div class="modify-salesman-dialog">
    <el-dialog
      width="800px"
      class=""
      :append-to-body="true"
      top="6vh"
      title="修改"
      :visible="$store.state.dialog.modifySalesman"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-card shadow="hover">
            <el-form :model="salesmanForm" label-width="100px" size="mini" :rules="rules" ref="salesmanForm">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="中文名" prop="fullName">
                    <el-input v-model="salesmanForm.fullName" :disabled="true" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="salesmanForm.sex">
                      <el-radio label="1">男</el-radio>
                      <el-radio label="2">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="重置密码">
                    <el-switch v-model="isUpdPassword" active-color="#13ce66"></el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="QQ" prop="qq">
                    <el-input v-model="salesmanForm.qq" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="电话号码" prop="phone1">
                    <el-input v-model="salesmanForm.phone1"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="手机号码" prop="mobile">
                    <el-input v-model="salesmanForm.mobile"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="部门" prop="jobId">
                    <el-select placeholder="请选择部门" v-model="salesmanForm.jobId" >
                      <el-option v-for="item in jobList" :key="item.jobId" :label="item.jobName" :value="item.jobId"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="状态">
                    <el-select placeholder="请选择" v-model="salesmanForm.state">
                      <el-option label="激活" value="1"></el-option>
                      <el-option label="冻结" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="登录用户名" prop="userName">
                    <el-input v-model="salesmanForm.userName" disabled="disabled"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-if="isUpdPassword">
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="salesmanForm.password"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-if="isUpdPassword">
                  <el-form-item label="确认密码" prop="makeSurePassword">
                    <el-input v-model="salesmanForm.makeSurePassword"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSalesman('salesmanForm')" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updSalesman } from '../api/userman'
export default {
  data () {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号码不能为空'))
      } else {
        const reg = /^([1-9]\d{10})$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    // var checkPhone2 = (rule, value, callback) => {
    //   const reg = /0\d{2}-\d{7,8}/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的座机号'))
    //   }
    // }
    var checkPassworder = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9]{6,11}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入长度在 6 到 11 个规范的密码'))
        }
      }
    }
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9_-]{4,16}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入长度在 4 到 16 位规范的用户名'))
        }
      }
    }
    var surePassworder = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.salesmanForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var checkQQ = (rule, value, callback) => {
      const reg = /^[1-9][0-9]{4,9}$/
      if (reg.test(value) || value === '') {
        callback()
      } else {
        return callback(new Error('请输入正确的QQ号'))
      }
    }
    return {
      isUpdPassword: false,
      salesmanForm: {},
      rules: {
        fullName: [
          { required: true, message: '请输入中文姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, validator: checkUsername, trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        jobId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        mobile: [
          { required: true, validator: checkPhone, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: checkPassworder, trigger: 'blur' }
        ],
        makeSurePassword: [
          { required: true, validator: surePassworder, trigger: 'blur' }
        ],
        qq: [
          { validator: checkQQ, trigger: 'blur' }
        ]
        // phone1: [
        //   { validator: checkPhone2, trigger: 'blur' }
        // ]
      }
    }
  },
  props: {
    myData: {
      type: String,
      default: () => {
        return ''
      }
    },
    jobList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  watch: {
    '$store.state.dialog.modifySalesman' (value) {
      if (value) {
        this.salesmanForm = JSON.parse(this.myData)
      }
    }
  },
  methods: {
    // 更新业务员数据表
    async editDataTab () {
      if (!this.isUpdPassword) {
        this.salesmanForm.password = ''
        this.salesmanForm.makeSurePassword = ''
      }
      if (this.salesmanForm.state === '激活') {
        this.salesmanForm.state = 1
      } else if (this.salesmanForm.state === '冻结') {
        this.salesmanForm.state = 0
      }
      console.log(typeof this.salesmanForm.state, this.salesmanForm.state)
      const res = await updSalesman({
        userName: this.salesmanForm.userName,
        password: this.salesmanForm.password,
        makeSurePassword: this.salesmanForm.makeSurePassword,
        mobile: this.salesmanForm.mobile,
        fullName: this.salesmanForm.fullName,
        state: this.salesmanForm.state,
        sex: this.salesmanForm.sex,
        qq: this.salesmanForm.qq,
        phone: this.salesmanForm.phone1,
        salesmanId: this.salesmanForm.salesmanId,
        isUpdPassword: this.isUpdPassword,
        jobId: this.salesmanForm.jobId
      })
      if (res.status === 200) {
        this.isUpdPassword = false
        // if (this.salesmanForm.sex === 1) {
        //   this.salesmanForm.sex = '男'
        // }
        // if (this.salesmanForm.sex === 2) {
        //   this.salesmanForm.sex = '女'
        // }
        // if (this.salesmanForm.state === 0) {
        //   this.salesmanForm.state = '冻结'
        // }
        // if (this.salesmanForm.state === 1) {
        //   this.salesmanForm.state = '激活'
        // }
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        this.$parent.getTabeDate(1, 15)
        this.$store.commit('hide_modify_salesman_cost')
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
      }
    },
    // 确认修改
    editSalesman (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.editDataTab()
        } else {
          return false
        }
      })
    },
    close () {
      this.$store.commit('hide_modify_salesman_cost')
    }
  }
}
</script>

<style scoped lang="less">

</style>
